package com.zhdl.modules.demo.DCHTProcess;

import com.zhdl.modules.demo.DCHTProcess.camera.CameraInitService;
import com.zhdl.modules.demo.DCHTProcess.camera.CameraInterface;
import com.zhdl.modules.process.common.AfterCurrentProcessDo;
import com.zhdl.modules.process.dto.ProcessBean;
import com.zhdl.modules.process.service.ProjectConfigProcessService;
import com.zhdl.modules.process.task.CustomizeTask;
import com.zhdl.modules.process.task.DynamicTask;
import com.zhdl.modules.process.verify.DelayTimeVerify;
import com.zhdl.modules.process.verify.PlCBitValueVerify;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.zhdl.modules.demo.DCHTProcess.CHTRobotInterface.PICK_LOCATION_DONE;

@Service
@Slf4j
public class MainProcessInit {
    @Resource
    RobotTestProcess robotTestProcess;
    @Resource
    ProjectConfigProcessService configurationPLcTest;
    @Resource
    DynamicTask dynamicTask;
    @Resource
    CHTSmallRobotInterface smallRobotInterface;
    @Resource
    CHTRobotInterface chtRobotInterface;

    @Resource
    CameraInitService cameraInitService;
    @Resource
    CameraInterface cameraInterface;
    private String cameraResult = "";

    public List<ProcessBean> initMain(DCTMainProcess dctMainProcess) {
        List<ProcessBean> processBeanList = new ArrayList<>();
        float step = 2.0f;
        chtRobotInterface.setCurrentNum(dctMainProcess.doneCount + 2.0f);
//        for (int i = 0; i < 3; i++) {
//            processBeanList.add(robotTestProcess.getProcessByStep(step + i, i));
//        }
        ProcessBean processByStep2 = robotTestProcess.getProcessByStep(2.0f, 0);
        processBeanList.add(processByStep2);

        processBeanList.add(
                new ProcessBean("拍照位置1通知相机拍照等待结果", 1, 5000)
                        .addDoSomeThing(() -> {
                            cameraInterface.upDataValue(CameraInterface.CAMERA_RESULT1, "0");
                            cameraInitService.takePhotoAction(1, 1, s -> {
                                cameraResult = s;
                                cameraInterface.upDataValue(CameraInterface.CAMERA_RESULT1, "1");
                            });
                        })
                        .addAfterProcess(lastProcess -> {
                            if (cameraResult.contains("ng")) {
                                dctMainProcess.doneCount++;
                                chtRobotInterface.setCurrentNum(dctMainProcess.doneCount + 2.0f);
                                chtRobotInterface.setCameraResult(false,"");
                                dctMainProcess.startProcess(0);
                            } else {
                                chtRobotInterface.setCameraResult(true,"");
                                dctMainProcess.startProcess(lastProcess+1);
                            }
                        })
                        .addProcessVerify(new PlCBitValueVerify(cameraInterface, CameraInterface.CAMERA_RESULT1)));

        ProcessBean processByStep3 = robotTestProcess.getProcessByStep(3.0f, 2);
        processBeanList.add(processByStep3);


        processBeanList.add(
                new ProcessBean("拍照位置2通知相机拍照等待结果", 3, 5000)
                        .addDoSomeThing(() -> {
                            cameraInterface.upDataValue(CameraInterface.CAMERA_RESULT1, "0");
                            cameraInitService.takePhotoAction(1, 2, s -> {
                                cameraResult = s;
                                cameraInterface.upDataValue(CameraInterface.CAMERA_RESULT1, "1");
                            });
                        })
                        .addAfterProcess(lastProcess -> {
                            if (cameraResult.contains("ng")) {
                                dctMainProcess.doneCount++;
                                chtRobotInterface.setCurrentNum(dctMainProcess.doneCount + 2.0f);
                                chtRobotInterface.setCameraResult(false,"");
                                dctMainProcess.startProcess(0);

                            } else {
                                try {
                                    chtRobotInterface.setCameraResult(true,cameraResult.split("_")[1]);
                                    dctMainProcess.startProcess(lastProcess+1);
                                }catch (Exception e){
                                    dctMainProcess.startProcess(lastProcess+1);
                                }
                            }
                        })
                        .addProcessVerify(new PlCBitValueVerify(cameraInterface, CameraInterface.CAMERA_RESULT1)));


      processBeanList.add(
              new ProcessBean("等待机器人到抓取点", 4, 500)
                              .addDoSomeThing(() -> {
                                  log.info("等待机器人到抓取点");
                              })
                              .addProcessVerify(new PlCBitValueVerify(chtRobotInterface,PICK_LOCATION_DONE)));
        processBeanList.add(
                new ProcessBean("控制机器人夹具夹紧", 5, 500)
                                .addDoSomeThing(() -> {
                                    new CylinderControlDo("SV11_机器人位夹爪",true);
                                })
                                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest,"机器人位夹爪气缸开到位")));

        processBeanList.add(robotTestProcess.getProcessByStep(4.0f,6));

        ProcessBean processByStep = robotTestProcess.getProcessByStep(5.0f, 7);
        processByStep.addAfterProcess(dctMainProcess::startTransport);
        processBeanList.add(processByStep);

        ProcessBean processByStep1 = robotTestProcess.getProcessByStep(6.0f, 4);
        processByStep1.addAfterProcess(dctMainProcess::startTransport);
        processBeanList.add(processByStep1);

        ProcessBean processByStep6 = robotTestProcess.getProcessByStep(7.0f, 5);
        processByStep6.addAfterProcess(dctMainProcess::startTransport);
        processBeanList.add(processByStep6);

        ProcessBean processByStep8 = robotTestProcess.getProcessByStep(8.0f, 6);
        processByStep8.addAfterProcess(dctMainProcess::startTransport);
        processBeanList.add(processByStep8);

        ProcessBean processByStep9 = robotTestProcess.getProcessByStep(9.0f, 7);
        processByStep9.addAfterProcess(dctMainProcess::startTransport);
        processBeanList.add(processByStep9);

        ProcessBean processByStep10 = robotTestProcess.getProcessByStep(10, 8);
        processByStep10.addAfterProcess(dctMainProcess::startTransport);
        processBeanList.add(processByStep10);

        processBeanList.add(robotTestProcess.getProcessByStep(11, 9));

        processBeanList.add(robotTestProcess.getProcessByStep(12, 13));

//        processBeanList.add(new ProcessBean("打螺丝位横移轴 移动至等待位", 23)
//                .addDoSomeThing(new MotorABSRunDo("打螺丝位横移轴", 0))
//                .addProcessVerify(new DistanceVerify(Float.parseFloat(configurationPLcTest.getValue("打螺丝位横移轴_MoveAbs_Pos[0]").getValue()), 0.1f, configurationPLcTest, "打螺丝位横移轴_CurrentPos"))
//                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "打螺丝位横移轴_MoveAbs_Done[0]")));
        processBeanList.add(new ProcessBean("SV9_旋转夹紧位夹爪松开", 11)
                .addDoSomeThing(new CylinderControlDo("SV9_旋转夹紧位夹爪", false))
                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "旋转夹紧位夹爪气缸关到位")));


        processBeanList.add(new ProcessBean("旋转横移电机运动至工作位", 12, 500)
                .addDoSomeThing(() -> {
                    log.info("控制横移电机运动至工作位");
                })
                .addProcessVerify(new DelayTimeVerify(3000)));

        processBeanList.add(new ProcessBean("SV9_旋转夹紧位夹爪夹紧", 13)
                .addDoSomeThing(new CylinderControlDo("SV9_旋转夹紧位夹爪", true))
                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "旋转夹紧位夹爪气缸开到位")));


        processBeanList.add(
                new ProcessBean("控制机器人夹具松开", 4, 500)
                        .addDoSomeThing(() -> {
                            new CylinderControlDo("SV11_机器人位夹爪",false);
                        })
                        .addProcessVerify(new PlCBitValueVerify(configurationPLcTest,"机器人位夹爪气缸关到位")));




        processBeanList.add(robotTestProcess.getProcessByStep(13, 14));
        processBeanList.add(new ProcessBean("打螺丝电机运动至相机拍照位", 15, 500)
                .addDoSomeThing(() -> {
                    log.info("打螺丝电机运动至相机拍照位 通知相机程序拍照等待相机结果");
                })
                .addAfterProcess(lastProcess -> {
                    ProcessBean processBean = new ProcessBean("旋转电机根据视觉结果旋转", 0, 500)
                            .addDoSomeThing(() -> {
                                log.info("根据相机识别结果旋转,如果旋转到位直接进入下一步,否则重复拍照");
                                dctMainProcess.startTransport(16);
                            })
                            .addProcessVerify(new DelayTimeVerify(3000));
                    processBean.getProcessDo().doSomeThing();
                    dynamicTask.startTask(new CustomizeTask(processBean), 100);
                })
                .addProcessVerify(new DelayTimeVerify(3000)));

        processBeanList.add(new ProcessBean("打螺丝横移轴移动至等待位", 16, 500)
                .addDoSomeThing(() -> {
                    log.info("打螺丝横移轴移动至等待位");
                })
                .addProcessVerify(new DelayTimeVerify(3000)));

        processBeanList.add(new ProcessBean("控制四轴机器人上料", 17, 500)
                .addDoSomeThing(() -> {
                    log.info("控制四轴机器人上料");
                    chtSmallRobotInterface.moveByStep();
                })
                .addProcessVerify(new PlCBitValueVerify(smallRobotInterface, smallRobotInterface.PICK_DONE)));

        processBeanList.add(new ProcessBean("控制四轴机器人上料", 18, 500)
                .addDoSomeThing(() -> {
                    log.info("控制四轴机器人上料");
                    chtSmallRobotInterface.moveByStep();
                })
                .addProcessVerify(new PlCBitValueVerify(smallRobotInterface, smallRobotInterface.PICK_DONE)));

        processBeanList.add(new ProcessBean("打螺丝横移轴移动至打螺丝位置1 Z轴下降 触发打螺丝机 打完螺丝Z轴上升", 19, 11500)
                .addDoSomeThing(() -> {
                    dctMainProcess.startDLS(0);
                })
                .addAfterProcess(lastProcess -> smallRobotInterface.getValue(CHTSmallRobotInterface.DLS_DONE).setValue("0"))
                .addProcessVerify(new PlCBitValueVerify(smallRobotInterface, CHTSmallRobotInterface.DLS_DONE)));
        processBeanList.add(new ProcessBean("打螺丝横移轴移动至打螺丝位置1 Z轴下降 触发打螺丝机 打完螺丝Z轴上升", 19, 11500)
                .addDoSomeThing(() -> {
                    dctMainProcess.startDLS(1);
                })
                .addAfterProcess(lastProcess -> smallRobotInterface.getValue(CHTSmallRobotInterface.DLS_DONE).setValue("0"))
                .addProcessVerify(new PlCBitValueVerify(smallRobotInterface, CHTSmallRobotInterface.DLS_DONE)));
        processBeanList.add(new ProcessBean("打螺丝横移轴移动至打螺丝位置1 Z轴下降 触发打螺丝机 打完螺丝Z轴上升", 19, 11500)
                .addDoSomeThing(() -> {
                    dctMainProcess.startDLS(2);
                })
                .addAfterProcess(lastProcess -> smallRobotInterface.getValue(CHTSmallRobotInterface.DLS_DONE).setValue("0"))
                .addProcessVerify(new PlCBitValueVerify(smallRobotInterface, CHTSmallRobotInterface.DLS_DONE)));
        processBeanList.add(new ProcessBean("打螺丝横移轴移动至打螺丝位置1 Z轴下降 触发打螺丝机 打完螺丝Z轴上升", 19, 11500)
                .addDoSomeThing(() -> {
                    dctMainProcess.startDLS(3);
                })
                .addAfterProcess(lastProcess -> smallRobotInterface.getValue(CHTSmallRobotInterface.DLS_DONE).setValue("0"))
                .addProcessVerify(new PlCBitValueVerify(smallRobotInterface, CHTSmallRobotInterface.DLS_DONE)));
        processBeanList.add(
                new ProcessBean("打螺丝横移轴回到等待位", 20, 500)
                        .addDoSomeThing(() -> {
                            //new MotorABSRunDo("旋转夹紧位横移轴",0);
                        })
                        .addProcessVerify(new DelayTimeVerify(2000)));
        processBeanList.add(robotTestProcess.getProcessByStep(14, 21));

        processBeanList.add(
                new ProcessBean("控制机器人夹具夹紧", 4, 500)
                        .addDoSomeThing(() -> {
                            new CylinderControlDo("SV11_机器人位夹爪",true);
                        })
                        .addProcessVerify(new PlCBitValueVerify(configurationPLcTest,"机器人位夹爪气缸开到位")));



        processBeanList.add(new ProcessBean("SV9_旋转夹紧位夹爪松开", 22)
                .addDoSomeThing(new CylinderControlDo("SV9_旋转夹紧位夹爪", false))
                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "旋转夹紧位夹爪气缸关到位")));
        processBeanList.add(robotTestProcess.getProcessByStep(15, 23));
        processBeanList.add(robotTestProcess.getProcessByStep(16, 24));
        processBeanList.add(
                new ProcessBean("控制夹具打开", 1, 500)
                                .addDoSomeThing(() -> {
                                    new CylinderControlDo("SV11_机器人位夹爪",false);
                                })
                                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest,"旋转夹紧位夹爪气缸关到位")));
        processBeanList.add(robotTestProcess.getProcessByStep(100, 24));
        return processBeanList;
    }

    @Resource
    CHTSmallRobotInterface chtSmallRobotInterface;

    public List<ProcessBean> initTransportTHQ() {
        List<ProcessBean> list = new ArrayList<>();
        list.add(new ProcessBean("搬運觸指流程開始", 0, 500)
                .addDoSomeThing(() -> {
                    log.info("搬運觸指流程開始....");
                })
                .addProcessVerify(new DelayTimeVerify(200)));
        list.add(new ProcessBean("SV4_触指顶升气缸打开", 1)
                .addDoSomeThing(new CylinderControlDo("SV4_触指顶升气缸", true))
                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "触指顶升气缸开到位")));


        list.add(new ProcessBean("默认机器人已经取料固定至套环装配位,SV1_1#料仓 SV2_2#料仓气缸控关闭", 2)
                .addDoSomeThing(() -> {
                    new CylinderControlDo("SV1_1#料仓夹紧气缸", true).doSomeThing();
                    new CylinderControlDo("SV2_2#料仓夹紧气缸", true).doSomeThing();
                })
                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "1#料仓夹紧气缸开到位"))
                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "2#料仓夹紧气缸开到位")));
        list.add(new ProcessBean("默认机器人已经取料固定至套环装配位,SV1_1#料仓 SV2_2#料仓气缸控制打开", 3)
                .addDoSomeThing(() -> {
                    new CylinderControlDo("SV1_1#料仓夹紧气缸", false).doSomeThing();
                    new CylinderControlDo("SV2_2#料仓夹紧气缸", false).doSomeThing();
                })
                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "1#料仓夹紧气缸关到位"))
                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "2#料仓夹紧气缸关到位")));
        list.add(new ProcessBean("SV4_触指顶升气缸关闭", 4)
                .addDoSomeThing(new CylinderControlDo("SV4_触指顶升气缸", false))
                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "触指顶升气缸关到位")));
        list.add(new ProcessBean("SV3_触指横移气缸打开", 5)
                .addDoSomeThing(new CylinderControlDo("SV3_触指横移气缸", true))
                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "触指横移气缸开到位")));
//        list.add(new ProcessBean("SV7_套环横移关闭", 6)
//                .addDoSomeThing(new CylinderControlDo("SV7_套环横移气缸", false))
//                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "套环横移气缸关到位")));
        list.add(new ProcessBean("SV6_套环提升打开运动至取触指位置", 6)
                .addDoSomeThing(new CylinderControlDo("SV6_套环提升气缸", true))
                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "套环提升气缸关到位")));
        list.add(new ProcessBean("SV5_触指撑开打开", 7)
                .addDoSomeThing(new CylinderControlDo("SV5_触指撑开气缸", true))
                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "撑开气缸开到位")));
        list.add(new ProcessBean("SV6_套环提升关闭-提升", 8)
                .addDoSomeThing(new CylinderControlDo("SV6_套环提升气缸", false))
                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "套环提升气缸开到位")));
        list.add(new ProcessBean("SV7_套环横移打开运动至套环位置", 9)
                .addDoSomeThing(() -> {
                    new CylinderControlDo("SV7_套环横移气缸", true).doSomeThing();
                    new CylinderControlDo("SV3_触指横移气缸", false).doSomeThing();
                })
                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "套环横移气缸开到位"))
                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "触指横移气缸关到位")));
        list.add(new ProcessBean("SV6_套环提升打开运动至取触指位置", 10)
                .addDoSomeThing(new CylinderControlDo("SV6_套环提升气缸", true))
                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "套环提升气缸关到位")));
        list.add(new ProcessBean("SV8_套触指气缸关闭夹紧", 11)
                .addDoSomeThing(new CylinderControlDo("SV8_套触指气缸", true))
                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "套触指气缸关到位")));
        list.add(new ProcessBean("SV5_触指撑开关闭", 12)
                .addDoSomeThing(new CylinderControlDo("SV5_触指撑开气缸", false))
                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "撑开气缸开到位")));
        list.add(new ProcessBean("SV6_套环提升关闭-提升", 13)
                .addDoSomeThing(new CylinderControlDo("SV6_套环提升气缸", false))
                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "套环提升气缸开到位")));
        list.add(new ProcessBean("SV7_套环横移关闭", 14)
                .addDoSomeThing(() -> {
                    new CylinderControlDo("SV7_套环横移气缸", false).doSomeThing();
                    new CylinderControlDo("SV8_套触指气缸", false).doSomeThing();
                })
                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "套环横移气缸关到位"))
                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "套触指气缸开到位")));
        list.add(new ProcessBean("套觸指流程結束", 15)
                .addDoSomeThing(() -> log.info("流程结束标记"))
                .addProcessVerify(new DelayTimeVerify(100)));
        return list;
    }
}
